package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.dao.RoomDao;
import com.neusoft.entity.Room;
import com.neusoft.service.RoomService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomDao roomDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Room queryById(@Param("id")Integer id) {
        return this.roomDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public IPage<Room> queryAllByLimit(int offset, int limit, Room bean) {
        Page<Room> page = new Page<>(offset,limit);
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.lt("amount",bean.getAmount())
                .like("location",bean.getLocation()).
                like("type",bean.getType());
        page.setRecords(roomDao.queryAll(page,bean));
        return page;
    }

    @Override
    public List<Room> queryAll(Room bean) {
        return null;
    }

    /**
     * 新增数据
     *
     * @param rent 实例对象
     * @return 实例对象
     */
    @Override
    public Room insert(Room rent) {
        this.roomDao.insert(rent);
        return rent;
    }

    /**
     * 修改数据
     *
     * @param rent 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Room rent) {
        return this.roomDao.update(rent);
    }

    @Override
    public boolean deleteById(List<Integer> ids) {
        if(ids == null || ids.size()==0){
            return false;
        }
        StringBuffer sb = new StringBuffer("id in (");
        for(Integer item:ids){
            sb.append("'");
            sb.append(item);
            sb.append("',");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(")");
        return this.roomDao.delete("stu.room",sb.toString()) > 0;
    }

    @Override
    public boolean deleteById1(@Param("id")Integer id) {
        if(id == null){
            return false;
        }
        StringBuffer sb = new StringBuffer("id =");
        sb.append(id);
        System.out.println(sb.toString());
        return this.roomDao.delete1("stu.room",sb.toString()) > 0;
    }
}
